package com.logic.modular.act.battle;

import com.comment.util.message.ResponseResult;
import com.comment.exception.ServiceException;
import com.logic.comment.util.JwtUtils;
import com.logic.modular.act.battle.vo.BattleVo;
import com.comment.pojo.domain.logic.domain.PlayerHero;
import com.comment.pojo.domain.logic.domain.PvpRanks;
import com.logic.modular.game.player.info.service.PlayerStatusServiceImpl;
import com.logic.modular.game.player.match.service.PvpRanksServiceImpl;
import com.logic.modular.game.player.hero.service.PlayerHeroServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.*;

/**
 * 武将队伍(PvpRanks)表服务控制层
 *
 * @author cxy
 * @since 2024-07-18 11:19:32
 */
@Api(tags = "pvp武将配队")
@Validated
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/pvpRanksAct")
public class PvpRanksAction {
    @Resource
    private PlayerHeroServiceImpl accountWuJiangService;
    @Resource
    private PvpRanksServiceImpl pvpRanksService;
    @Resource
    private PlayerStatusServiceImpl playerStatusServiceImpl;

    @ApiOperation("队伍列表")
    @PostMapping("/list")
    public ResponseResult<List<PvpRanks>> list(@RequestHeader String token) {
        Long playerId = JwtUtils.getPlayerId(token);
        return ResponseResult.success(pvpRanksService.obtainPvpRanKs(playerId));
    }

    @ApiOperation("卸载武将")
    @PostMapping("/uninstallation")
    public ResponseResult<PvpRanks> uninstallation(
            @RequestHeader String token, @RequestBody BattleVo.PvpRanksVo.uninstallation param

    ) {
        return ResponseResult.success(pvpRanksService.uninstallation(
                JwtUtils.getPlayerId(token), param.getRankNo(), param.getRankOrder()
        ));
    }

    @ApiOperation("队伍位置编辑")
    @PostMapping("/teamPositionEditing")
    public ResponseResult<PvpRanks> teamPositionEditing(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.teamPositionEditing param
    ) {
        return ResponseResult.success(pvpRanksService.teamPositionEditing(
                JwtUtils.getPlayerId(token), param.getRankNo(), param.getRankOrder())
        );
    }

    @ApiOperation("编队")
    @PostMapping("/formation")
    public ResponseResult<List<PvpRanks>> formation(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.Formation param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Integer rankNo = param.getRankNo();
        Long heroId1 = param.getHeroId1();
        Long heroId2 = param.getHeroId2();
        Long heroId3 = param.getHeroId3();

        // 去重
        if (Objects.equals(heroId1, heroId2) && heroId1 != 0
                || Objects.equals(heroId1, heroId3) && heroId1 != 0
                || Objects.equals(heroId2, heroId3) && heroId2 != 0
        ) {
            throw new ServiceException("请勿重复选择武将");
        }
        return ResponseResult.success(pvpRanksService.formation(playerId, Math.toIntExact(rankNo), heroId1, heroId2, heroId3));
    }

    @ApiOperation("编队")
    @PostMapping("/formation2")
    public ResponseResult<List<PvpRanks>> formation2(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.Formation2 param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Integer rankNo = param.getRankNo();
        Long[][] rankInfo = param.getRankInfo();
        return ResponseResult.success(pvpRanksService.formation2(playerId, rankNo, rankInfo));
    }

    @ApiOperation("出战")
    @PostMapping("/clickBattle")
    public ResponseResult<PvpRanks> clickBattle(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.ToCityVo param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long matchId = playerStatusServiceImpl.currentMatchId(playerId);
        if (matchId == 0) {
            throw new ServiceException("当前没有对局");
        }
        return ResponseResult.success(accountWuJiangService.clickBattle(
                playerId, matchId, param.getRankNo(), param.getTargetCityId())
        );
    }

    @ApiOperation("4. 队伍移动")
    @PostMapping("/teamMovement")
    public ResponseResult<PvpRanks> teamMovement(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.ToCityVo param
    ) {
        return ResponseResult.success("返回队伍,到达时间", pvpRanksService.teamMovement(
                JwtUtils.getPlayerId(token), Math.toIntExact(param.getRankNo()), param.getTargetCityId())
        );
    }

    @ApiOperation("获取队伍武将")
    @PostMapping("/obtainTeamHero")
    public ResponseResult<?> obtainTeamHero(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.RankNo param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Integer rankNo = param.getRankNo();
        List<PlayerHero> wuJiang = pvpRanksService.getWuJiang(playerId, Math.toIntExact(rankNo));
        return ResponseResult.success("ok", wuJiang);
    }


    @ApiOperation("攻打城池")
    @PostMapping("/attackCity")
    public ResponseResult<?> attackCity(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.ToCityVo param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long currentMatchId = playerStatusServiceImpl.currentMatchId(playerId);
        Integer rankNo = param.getRankNo();
        Long targetCityId = param.getTargetCityId();
        pvpRanksService.tryAttackCity(playerId, currentMatchId, rankNo, targetCityId);
        return ResponseResult.success("ok");
    }

    @ApiOperation("援护队友城池")
    @PostMapping("/intervene")
    public ResponseResult<?> tryInterveneTargetCity(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.ToCityVo param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Long currentMatchId = playerStatusServiceImpl.currentMatchId(playerId);
        Integer rankNo = param.getRankNo();
        Long targetCityId = param.getTargetCityId();
        return ResponseResult.success("ok", pvpRanksService.tryInterveneTargetCity(playerId, currentMatchId, rankNo, targetCityId));
    }

    @ApiOperation("移动加速")
    @PostMapping("/marchingAcceleration")
    public ResponseResult<?> marchingAcceleration(
            @RequestHeader String token,
            @Valid @RequestBody BattleVo.PvpRanksVo.RankNo param
    ) {
        Long playerId = JwtUtils.getPlayerId(token);
        Integer rankNo = param.getRankNo();
        pvpRanksService.marchingAcceleration(playerId, rankNo);
        return ResponseResult.success("ok");
    }
}
